# encoding:utf8
''' 2017-8-24 pylint fix '''
import os


class GitHelper(object):

    def __init__(self, user, pwd):
        self._user = user
        self._pwd = pwd
        self._branch = "develop"  # 默认就是develop

    def git_branch(self, tarpath_, branch):
        os.chdir(tarpath_)
        self._branch = branch
        cmd_str = "git checkout %s" % branch
        ret = os.popen(cmd_str)
        print(ret.read())

    def git_push(self, tarpath, shortlog="AUTO:Jenkins tools commit", singlefile=""):
        os.chdir(tarpath)
        cmd_add = '''git add .'''
        cmd_commit = '''git commit -m "{shortlog}" {singlefile}'''.format(
            shortlog=shortlog, singlefile=singlefile)
        cmd_push = '''git push origin %s''' % self._branch
        ret = os.popen(cmd_add)
        print(ret.read())

        ret1 = os.popen(cmd_commit)
        print(ret1.read())
        ret2 = os.popen(cmd_push)
        print(ret2.read())

    def git_pull(self, tarpath_):
        os.chdir(tarpath_)
        cmd_update = '''git pull origin %s''' % self._branch
        ret = os.popen(cmd_update).read()
        print(ret)
